package com.ynu.controller;
import com.ynu.bean.entity.Cart;
import com.ynu.bean.entity.CartItem;
import com.ynu.bean.entity.Goods;
import com.ynu.service.CartItemService;
import com.ynu.service.CartService;
import com.ynu.service.GoodsService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.List;


/**
* (tb_cart_item)表控制层
*
* @author xxxxx
*/
@RestController
@RequestMapping("/cartItem")
public class CartItemController {
    @Resource
    private GoodsService goodsService;

    @Resource
    private CartItemService cartItemService;

    @Resource
    private CartService cartService;

    @GetMapping("/addCartItem")
    public ModelAndView addCartItem(Long userId,Long goodsId,Integer amount){
        //购物项
        Goods goods = goodsService.getByPrimaryKey(goodsId);
        CartItem cartItem = CartItem.builder()
                .goodsId(goodsId)
                .amount(amount)
                .money(goods.getPrice2().multiply(new BigDecimal(amount)))
                .state(1)
                .build();
        //购物车
        Cart cart = cartService.selectByUserId(userId);
        if(cart ==null){
            //创建购物车
            cart = Cart.builder()
                    .userId(userId)
                    .totalMoney(goods.getPrice2().multiply(new BigDecimal(amount)))
                    .totalAmount(amount)
                    .build();
            cartService.createSelective(cart);
            //添加购物项
            cartItem.setCartId(cart.getId());
            cartItemService.insertSelective(cartItem);
        }else{
            //获取购物车中所有的购物项
            List<CartItem> cartItemList =  cartItemService.selectByCartId(cart.getId());
            //查看购物车中是否有当前添加的商品
            boolean flag = true; //没有
            for (CartItem tmp : cartItemList) {
                if(tmp.getGoodsId().equals(goodsId)){
                    //如果有，数量增加
                    tmp.setAmount( tmp.getAmount() +amount);
                    tmp.setMoney(goods.getPrice2().multiply(new BigDecimal(tmp.getAmount())));
                    //更新CartItem
                    cartItemService.updateByPrimaryKeySelective(tmp);
                    //更新Cart
                    cart.setTotalAmount( cart.getTotalAmount()+amount);
                    cart.setTotalMoney(cart.getTotalMoney().add(goods.getPrice2().multiply(new BigDecimal(amount))));
                    cartService.updateByPrimaryKeySelective(cart);
                    flag =false;
                    break;
                }
            }
            if(flag){
                //如果没有，添加购物项
                cartItem.setCartId(cart.getId());
                cartItemService.insertSelective(cartItem);
                //修改购物车
                cart.setTotalAmount(cart.getTotalAmount()+amount);
                cart.setTotalMoney(cart.getTotalMoney().add(goods.getPrice2().multiply(new BigDecimal(amount))));
                cartService.updateByPrimaryKeySelective(cart);
            }
        }

        return null;
    }


    @GetMapping("selectById")
    public CartItem selectById(Long id) {
        return cartItemService.selectByPrimaryKey(id);
    }

}
